//  [9/7/2013 OMEGA] created

#include "DuelAssECommon.h"
#include "DuelAssEProject.h"

DAssEResourceItem::DAssEResourceItem( DAssEArchive* parent, const QString& name ) :
	mParent(parent),
	mName(name),
	mbUseDefaultParameter(true),
	mTargetGroup(NULL)
{

}

DAssEResourceItem::DAssEResourceItem( const QString& name ) :
	mParent(NULL),
	mName(name),
	mbUseDefaultParameter(true),
	mTargetGroup(NULL)
{

}

DAssEResourceItem::~DAssEResourceItem()
{

}

QString DAssEResourceItem::getParameter( const QString& paramName )
{
	QString ret;
	ParameterMap::iterator i = mParameterMap.find(paramName);
	if (i != mParameterMap.end())
	{
		ret = i.value();
	}
	return ret;
}


DAssEResourceItem* DAssEArchive::getResourceItem( const QString& name )
{
	foreach(DAssEResourceItem* res, mAvailabeResourceList)
	{
		if (res->getName() == name)
		{
			return res;
		}
	}
	return NULL;
}


void DAssEArchive::destroyAllResourceItems()
{
	ResourceItemList::iterator i, iend = mAvailabeResourceList.end();
	for (i = mAvailabeResourceList.begin(); i != iend; ++i)
	{
		delete (*i);
	}
	mAvailabeResourceList.clear();
}

ResultCode DAssEArchive::addResourceItem( DAssEResourceItem* item )
{
	if (item == NULL)
	{
		return RC_RESOURCE_INVALID;
	}
	if (item->getParentArchive() == this)
	{
		return RC_RESOURCE_EXISTED;
	}
	item->mParent = this;
	mAvailabeResourceList.push_back(item);
	return RC_OK;
}

void DAssEArchive::removeResourceItem( DAssEResourceItem* item )
{
	ResourceItemList::iterator i, iend = mAvailabeResourceList.end();
	for (i = mAvailabeResourceList.begin(); i != iend; ++i)
	{
		if ((*i) == item)
		{
			item->mParent = NULL;
			mAvailabeResourceList.erase(i);
		}
	}
}

ResultCode DAssEGroup::attachArchive( DAssEArchive* archive )
{
	if (archive == NULL)
	{
		return RC_ARCHIVE_INVALID;
	}
	// check duplication
	if (isArchiveAttached(archive))
	{
		return RC_ARCHIVE_EXISTED;
	}
	mAttachedArchives.push_back(archive);
	return RC_OK;
}

void DAssEGroup::detachArchive( DAssEArchive* archive )
{
	ArchiveList::iterator i, iend = mAttachedArchives.end();
	for (i = mAttachedArchives.begin(); i != iend; ++i)
	{
		if ((*i) == archive)
		{
			mAttachedArchives.erase(i);
			return;
		}
	}
}

bool DAssEGroup::isArchiveAttached( DAssEArchive* archive )
{
	foreach (DAssEArchive* arch, mAttachedArchives)
	{
		if (arch == archive)
		{
			return true;
		}
	}
	return false;
}

ResultCode DAssEGroup::declareResource( DAssEResourceItem* item )
{
	if (item == NULL)
	{
		return RC_RESOURCE_INVALID;
	}
	// check duplication
	if (isResourceDeclared(item))
	{
		return RC_RESOURCE_EXISTED;
	}
	item->setDeclareTarget(this);
	mDeclaredResItems.push_back(item);
	return RC_OK;
}

void DAssEGroup::undeclareResource( DAssEResourceItem* item )
{
	ResourceItemList::iterator i, iend = mDeclaredResItems.end();
	for (i = mDeclaredResItems.begin(); i != iend; ++i)
	{
		if ((*i) == item)
		{
			mDeclaredResItems.erase(i);
			return;
		}
	}
}

bool DAssEGroup::isResourceDeclared( DAssEResourceItem* item )
{
	foreach(DAssEResourceItem* res, mDeclaredResItems)
	{
		if (res == item)
		{
			return true;
		}
	}
	return false;
}

DAssEProject::DAssEProject()
{
	mResDescFileNames[DAssEResourceItem::DAssERIT_MESH] = tr("MeshResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_TEXTURE] = tr("TextureResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_AUDIO] = tr("AudioResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_FONT] = tr("FontResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_FX] = tr("FxResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_ANIMATION] = tr("AnimationResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_SKELETON] = tr("SkeletonResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_SHADER] = tr("ShaderResources.xml");
	mResDescFileNames[DAssEResourceItem::DAssERIT_RENDEREFFECT] = tr("RenderEffectResources.xml");

	//
	ParameterMap meshParam;
	meshParam[tr("File Format")] = tr(".dm");
	mDefaultParamMap[DAssEResourceItem::DAssERIT_MESH] = meshParam;

	ParameterMap texParam;
	texParam[tr("File Format")] = tr(".dds");
	texParam[tr("Memory Strategy")] = tr("Dynamic");
	mDefaultParamMap[DAssEResourceItem::DAssERIT_TEXTURE] = texParam;

	ParameterMap audioParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_AUDIO] = audioParam;

	ParameterMap fontParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_FONT] = fontParam;

	ParameterMap fxParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_FX] = fxParam;

	ParameterMap animParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_ANIMATION] = animParam;

	ParameterMap skeParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_SKELETON] = skeParam;

	ParameterMap shaderParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_SHADER] = shaderParam;

	ParameterMap dreParam;
	mDefaultParamMap[DAssEResourceItem::DAssERIT_RENDEREFFECT] = dreParam;
}

DAssEProject::~DAssEProject()
{
	clear();
}


ResultCode DAssEProject::addArchive( DAssEArchive* arch )
{
	if (arch == NULL)
	{
		return RC_ARCHIVE_INVALID;
	}
	foreach (DAssEArchive* a, mArchiveList)
	{
		if (a == arch || a->getName() == arch->getName())
		{
			return RC_ARCHIVE_EXISTED;
		}
	}
	mArchiveList.push_back(arch);
	return RC_OK;
}

DAssEArchive* DAssEProject::getArchive( const QString& name )
{
	foreach (DAssEArchive* arch, mArchiveList)
	{
		if (arch->getName() == name)
		{
			return arch;
		}
	}
	return NULL;
}



void DAssEProject::removeArchive( DAssEArchive* arch )
{
	ArchiveList::iterator i, iend = mArchiveList.end();
	for (i = mArchiveList.begin(); i != iend; ++i)
	{
		if ((*i) == arch)
		{
			mArchiveList.erase(i);
			return;
		}
	}
}

void DAssEProject::destroyAllArchives()
{
	ArchiveList::iterator i, iend = mArchiveList.end();
	for (i = mArchiveList.begin(); i != iend; ++i)
	{
		delete (*i);
	}
	mArchiveList.clear();
}

ResultCode DAssEProject::addGroup( DAssEGroup* grp )
{
	if (grp == NULL)
	{
		return RC_GROUP_INVALID;
	}
	foreach (DAssEGroup* g, mGroupList)
	{
		if ( g == grp || g->getName() == grp->getName() )
		{
			return RC_GROUP_EXISTED;
		}
	}
	mGroupList.push_back(grp);
	return RC_OK;
}

DAssEGroup* DAssEProject::getGroup( const QString& name )
{
	foreach(DAssEGroup* grp, mGroupList)
	{
		if (grp->getName() == name)
		{
			return grp;
		}
	}
	return NULL;
}



void DAssEProject::removeGroup( DAssEGroup* grp )
{
	GroupList::iterator i, iend = mGroupList.end();
	for (i = mGroupList.begin(); i != iend; ++i)
	{
		if ((*i) == grp)
		{
			mGroupList.erase(i);
			return;
		}
	}
}

void DAssEProject::destroyAllGroups()
{
	GroupList::iterator i, iend = mGroupList.end();
	for (i = mGroupList.begin(); i != iend; ++i)
	{
		delete (*i);
	}
	mGroupList.clear();
}

void DAssEProject::clear()
{
	mAssetDir = "";
	mDescDir = "";
	destroyAllArchives();
	destroyAllGroups();
}

void DAssEProject::setDescriptionFileName( DAssEResourceItem::ItemType type, const QString& fileName )
{
	mResDescFileNames[type] = fileName;
}

void DAssEProject::setArchiveDescriptionFileName( const QString& fileName )
{
	mOutArchiveDescFileName = fileName;
}

void DAssEProject::setGroupDescriptionFileName( const QString& fileName )
{
	mOutGroupDescFileName = fileName;
}

void DAssEProject::setPackingTargetDir( const QString& dir )
{
	mPackingDir = dir;
}

const QString& DAssEProject::getDescriptionFileName( DAssEResourceItem::ItemType type )
{
	return mResDescFileNames[type];
}

const QString& DAssEProject::getArchiveDescriptionFileName()
{
	return mOutArchiveDescFileName;
}

const QString& DAssEProject::getGroupDescriptionFileName()
{
	return mOutGroupDescFileName;
}

const QString& DAssEProject::getPackingTargetDir()
{
	return mPackingDir;
}

ParameterMap* DAssEProject::getDefaultResourceParameter( DAssEResourceItem::ItemType type )
{
	return &mDefaultParamMap[type];
}
